package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.system.entity.UserDepartmentEntity;
import  com.sq.universe.system.service.UserDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.List;

@RestController
@RequestMapping("/userDepartment")
@CrossOrigin
public class UserDepartmentController extends BaseController {

    @Autowired
    private UserDepartmentService userDepartmentService;

    /**
    * 查询数据
    * @param userDepartmentEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:userDepartment:query")
    public RespEntity query(UserDepartmentEntity userDepartmentEntity){
        try {
            return new RespEntity("查询成功",userDepartmentService.query(userDepartmentEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param userDepartmentEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:userDepartment:queryByPage")
    public RespEntity queryByPage(UserDepartmentEntity userDepartmentEntity){
        try {
            PageHelper.startPage(userDepartmentEntity.getPageNo(),userDepartmentEntity.getPageSize());
            List<UserDepartmentEntity> userDepartmentList = userDepartmentService.query(userDepartmentEntity);
            PageInfo<UserDepartmentEntity> pageInfo = new PageInfo<UserDepartmentEntity>(userDepartmentList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param userDepartmentEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:userDepartment:insert")
    public RespEntity insert(UserDepartmentEntity userDepartmentEntity){
        try {
            userDepartmentService.insert(userDepartmentEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param userDepartmentEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:userDepartment:update")
    public RespEntity update(UserDepartmentEntity userDepartmentEntity){
        try {
            userDepartmentService.update(userDepartmentEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param userDepartmentEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:userDepartment:delete")
    public RespEntity delete(UserDepartmentEntity userDepartmentEntity){
        try {
            if(userDepartmentEntity.getUdId() == null){
                return new RespEntity("删除失败");
            }
            userDepartmentService.logicalDelete(userDepartmentEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }
}